﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.controllers.world {
    import flash.events.*;
    import net.wooga.pets.loading.vos.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.models.world.vos.areas.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;
    import net.wooga.pets.models.world.*;

    public class ChargeMachineCommand extends Command {

        public var event:InfoEvent;
        public var world:WorldModel;
        public var userModel:UserModel;
        public var sunModel:SunModel;
        public var languageModel:LanguageModel;
        public var tutorialModel:TutorialModel;

        override public function execute():void{
            var _local1:Object;
            var _local2:Array;
            var _local3:TaskRequestEvent;
            if (this.userModel.batteries > 0){
                this.userModel.batteries = (this.userModel.batteries - 1);
                _local1 = {taskType:GameConstants.CHARGE_MACHINE, itemType:GameConstants.BATTERY, onResult:this.onCharge};
                _local2 = [new RequestVO("charge_machine", {id:this.event.id})];
                _local3 = new TaskRequestEvent(EventConstants.SEND_TASK_REQUEST, _local2, _local1);
                dispatch(_local3);
                this.createRewardAnimation();
            } else {
                if (this.tutorialModel.state > TutorialModel.LEVEL1_WELCOME_PATIENT_2){
                    dispatch(new InfoEvent(EventConstants.HIGHLIGHT_MACHINES, GameConstants.TYPE_BATTERY_ROOM));
                    this.sunModel.simpleMessage = this.languageModel.getTextByKey("sun_message_nobattery");
                };
                this.handleCallback(false);
            };
        }
        private function createRewardAnimation():void{
            var _local1:RoomVO = RoomVO(this.world.getRoomById(this.event.id));
            var _local2:Object = new Object();
            _local2[EventConstants.ITEM_TYPE] = GameConstants.REWARD_BATTERY;
            _local2[EventConstants.TARGET_ID] = _local1.machine.id;
            dispatch(new InfoEvent(EventConstants.CREATE_REWARD_ANIMATION_INTO_HOSPITAL, null, _local2));
            dispatch(new InfoEvent(EventConstants.PLAY_EFFECT_SOUND, GameConstants.SOUND_MENU_CLICK));
        }
        private function onCharge(_arg1:ResultVO):void{
            if (_arg1.success){
                dispatch(new InfoEvent(EventConstants.UPDATE_USER_DATA, null, _arg1.data.charge_machine));
            };
            this.handleCallback(_arg1.success);
        }
        private function handleCallback(_arg1:Boolean):void{
            var _local2:Function = (this.event.info as Function);
            _local2(_arg1);
            dispatch(new Event(EventConstants.HANDLE_NEXT_AVATAR_ACTION));
        }

    }
}//package net.wooga.pets.controllers.world 
